package com.overoid.diary;

import java.util.HashMap;

import com.overoid.diary.ui.MemoActivity;
import com.overoid.diary.ui.common.ActivityImpl;
import com.overoid.diary.ui.common.Constants;
import com.overoid.diary.ui.common.DBHelper;
import com.overoid.diary.ui.day.edit.DayEditActivity;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

public class MainActivity extends ActivityImpl {
	
	protected DBHelper db;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.main);
		
		init();
		
		
		// search btn click
		final ImageButton mainBtnSearch =  (ImageButton)findViewById(R.id.mainBtnSearch);
		mainBtnSearch.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent in = new Intent(MainActivity.this, MemoActivity.class);
				in.putExtra("searchWord", ((EditText)findViewById(R.id.mainTxtSearch)).getText().toString());
				startActivity(in);
			}
		});
		
		
		final ImageButton mainBtnCategoryLove =  (ImageButton)findViewById(R.id.mainBtnCategoryLove);
		mainBtnCategoryLove.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent in = new Intent(MainActivity.this, MemoActivity.class);
				in.putExtra("memo_class_id", 1);
				startActivity(in);
			}
		});
		
		final ImageButton mainBtnCategoryMusic =  (ImageButton)findViewById(R.id.mainBtnCategoryMusic);
		mainBtnCategoryMusic.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent in = new Intent(MainActivity.this, MemoActivity.class);
				in.putExtra("memo_class_id", 2);
				startActivity(in); 
			}
		});
		
		final ImageButton mainBtnCategorySad =  (ImageButton)findViewById(R.id.mainBtnCategorySad);
		mainBtnCategorySad.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent in = new Intent(MainActivity.this, MemoActivity.class);
				in.putExtra("memo_class_id", 3);
				startActivity(in);
			}
		});
		
		final ImageButton mainBtnCategoryTag =  (ImageButton)findViewById(R.id.mainBtnCategoryTag);
		mainBtnCategoryTag.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent in = new Intent(MainActivity.this, MemoActivity.class);
				in.putExtra("memo_class_id", 4);
				startActivity(in);
				 
			}
		});
		
		final Button mainBtnMemoList =  (Button)findViewById(R.id.mainBtnMemoList);
		mainBtnMemoList.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent in = new Intent(MainActivity.this, MemoActivity.class);
				startActivity(in);
			}
		});
		
		
		final Button mainBtnNewMemo =  (Button)findViewById(R.id.mainBtnNewMemo);
		mainBtnNewMemo.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent in = new Intent(MainActivity.this, DayEditActivity.class);
				startActivity(in);
			}
		});
	}
	
	private void init(){
		db = new DBHelper(getApplicationContext());
		Cursor c = db.getMemoClassListTatal();
		Constants.CATEGORY_IMAGE_MAP =  new HashMap<Integer, Integer>();
		c.moveToFirst();
		int imageId = 0;
		for(int i=0; i < c.getCount(); i++) {
			
			if(c.getInt(0) == 1 ){
				imageId = R.drawable.edit_tab01_bottom_category_love_48x48;
			}else if(c.getInt(0) == 2 ){
				imageId = R.drawable.edit_tab01_bottom_category_music_48x48;
			} else if(c.getInt(0) == 3 ){
				imageId = R.drawable.edit_tab01_bottom_category_sad_48x48;
			}else if(c.getInt(0) == 4 ){
				imageId = R.drawable.edit_tab01_bottom_category_tag_48x48;
			}
			
			Constants.CATEGORY_IMAGE_MAP.put(c.getInt(0),imageId);
			
			
			c.moveToNext();
		}
		
		if (c != null && !c.isClosed()) {
			c.close();
		}
	}
	
	
}
